<?php
class moduleModel extends baseModel{
    protected $table ='cms_module';  //模型表
    //获取模型表
    public function tables($mid){
        $mid = intval($mid);
        $tables = $this->find('id='.$mid,'tables');
        return $tables['tables'];
    }
    //删除模型->数据库表
    public function delTable($fid){
        $hackmid = model('channel')->find('mid='.$fid);
        if($hackmid){
            return '删除模型失败,请先删除已绑定模型的栏目';
        }
        $rel = model('field')->find('mid='.$fid);
        if($rel){
            return '删除模型失败,请先删除模型字段';
        }
        $info = $this->find('id='.$fid,'tables');  //获取当前模型信息
        $sql = "DROP TABLE ".config('DB_PREFIX')."{$info['tables']}";
        $result = $this->query($sql); //删除主表
        $sqls = "DROP TABLE ".config('DB_PREFIX')."{$info['tables']}_data";
        $result = $this->query($sqls); //删除副表
        if($result){
            $this->delete('id='.$fid);
            return 1;
        }else{
            return '删除模型失败';
        }
    }
    //增加模型->添加数据库表
    public function addModule($data = array()){
        $condition['tables'] = $data['tables'];
        $datas['tables'] = $condition['tables'] . '_data';
        $result = $this->find($condition); 
        if($result){
            return FALSE;
        }
        $type = intval($data['type']);
        if($type == 1){//资源模型扩展
            $sql = "CREATE TABLE IF NOT EXISTS ".config('DB_PREFIX')."{$data['tables']}(
            `id` int(11) NOT NULL auto_increment,
            `fid` int(10) NOT NULL,
            `cid` int(10) NOT NULL,
            `title` varchar(150) NOT NULL,
            `fontcolor` varchar(10) NOT NULL,
            `uptime` int(10) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
            $sqls = "CREATE TABLE IF NOT EXISTS ".config('DB_PREFIX')."{$datas['tables']}(
            `id` int(10) NOT NULL,
            `fid` int(10) NOT NULL,
            `cid` int(8) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
        }elseif($type == 2){//栏目扩展
            $sql = "CREATE TABLE IF NOT EXISTS ".config('DB_PREFIX')."{$data['tables']}(
            `id` int(11) NOT NULL auto_increment,
            `cid` int(10) NOT NULL,
            `uptime` int(10) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
            $sqls = "CREATE TABLE IF NOT EXISTS ".config('DB_PREFIX')."{$datas['tables']}(
            `id` int(10) NOT NULL,
            `cid` int(8) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
        }else{//资源模型主表
            $sql = "CREATE TABLE IF NOT EXISTS ".config('DB_PREFIX')."{$data['tables']}(
            `id` int(11) NOT NULL auto_increment,
            `cid` int(10) NOT NULL,
            `title` varchar(150) NOT NULL,
            `url` varchar(250) NOT NULL,
            `editor` varchar(100) NOT NULL,
            `sort` int(10) NOT NULL,
            `views` int(10) NOT NULL,
            `status` tinyint(1) NOT NULL,
            `special` varchar(250) NOT NULL,
            `fontcolor` varchar(10) NOT NULL,
            `uptime` int(10) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
            $sqls = "CREATE TABLE IF NOT EXISTS ".config('DB_PREFIX')."{$datas['tables']}(
            `id` int(10) NOT NULL,
            `cid` int(8) NOT NULL,
            PRIMARY KEY  (`id`)
            ) ENGINE=MyISAM DEFAULT CHARSET=utf8";
        }
        $info = $this->query($sql);
        if($info){
            $this->insert($data); 
            $this->query($sqls);
            return TRUE;
        }else{
            return FALSE;
        }
    }
    //编辑模型->数据库表->数据库表字段
    public function editModule($fid,$data = array()){
        $condition = array();
        $condition['id'] = $fid;
        $result = $this->update($condition,$data);
        if($result){
            return TRUE;
        }else{
            return FALSE;
        }
    }
}